<?php

namespace app\common\service;

class CommodityHandleService
{

    public static function getProductId($link){
        $ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/13.0 MQQBrowser/9.0.0 Mobile/15B87 Safari/604.1 MttCustomUA/2 QBWebViewType/1 WKType/1';
        $url = self::get_location($link, $ua);
        $url = urldecode($url);
        $re = self::getNeedBetween($url,"product_id","promotion_id");
        $re = preg_replace('/[^0-9-\+\.,]+/', '', $re);
        if(is_numeric($re)){
            return $re;
        }else{
            return false;
        }
    }


    public static function getLinkInfo($link){
        $ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/13.0 MQQBrowser/9.0.0 Mobile/15B87 Safari/604.1 MttCustomUA/2 QBWebViewType/1 WKType/1';
        $url = self::get_location($link, $ua);
        $url = urldecode($url);
        $re = parse_url($url);// 解析 URL，返回其组成部分
        if(empty($re['query'])||!isset($re['query'])){
            return false;
        }
        $arr_query = self::convertUrlQuery($re['query']);//将字符串参数拆解为数组
//        $product_id = $arr_query[""]
        if(empty($arr_query["goods_detail"])){
            return false;
        }
        $arr_query["url"] = $url;
        $arr_query["goods_detail"] = @self::object_array(@json_decode($arr_query["goods_detail"]));
        return $arr_query;
    }

    /** 对象转数组
     * @param $array object
    */
    public static function object_array($array){
        if(is_object($array)){
            $array = (array)$array;
        }
        if(is_array($array)){
            foreach($array as $key=>$value){
                $array[$key] = @self::object_array($value);
            }
        }
        return $array;
    }

    public static function get_location($url, $ua = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $httpheader[] = "Accept:*/*";
        $httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
        $httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
        $httpheader[] = "Connection:close";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
        curl_setopt($ch, CURLOPT_HEADER, true);
        if ($ua) {
            curl_setopt($ch, CURLOPT_USERAGENT, $ua);
        } else {
            curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0");
        }
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $ret = curl_exec($ch);
        curl_close($ch);
        preg_match("/Location: (.*?)\r\n/iU", $ret, $location);
        return $location[1];
    }



    public static function getNeedBetween($kw1,$mark1,$mark2){
        $kw=str_replace(' ','',$kw1);
        $st =stripos($kw,$mark1);
        $kw = substr($kw, $st);
        $ed =stripos($kw,$mark2);
        $kw = substr($kw, 0,$ed);
        if(($st==false||$ed==false)){
            return 0;
        }
        return $kw;
    }


    /**
     * 将字符串参数变为数组
     * @param $query
     * @return array
     */
    public static function convertUrlQuery($query)
    {
        $queryParts = explode('&', $query);
        $params = array();
        foreach ($queryParts as $param) {
            $item = explode('=', $param);
            $params[$item[0]] = $item[1];
        }
        return $params;
    }
    /**
     * 将参数变为字符串
     * @param $array_query
     * @return string
     */
    public static function getUrlQuery($array_query)
    {
        $tmp = array();
        foreach($array_query as $k=>$param) {
            $tmp[] = $k.'='.$param;
        }
        $params = implode('&',$tmp);
        return $params;
    }

}